﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using NabNetLib;

namespace NabNetService
{
    public partial class Main : ServiceBase
    {
        public Main()
        {
            InitializeComponent();
        }

        
        private TcpListener mListenerPing;
        private XmppListener mListener;
        private Scheduler mScheduler;
        

        protected override void OnStart(string[] args)
        {
            string sSource = "NabNetService";
            string sLog = "Application";
            

            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);

            EventLog.WriteEntry(sSource, "Starting");

            try
            {
                mListenerPing = new TcpListener(IPAddress.Any, 8080);
                mListenerPing.Start();

                NabNetLib.Remoting.RemoteNabNet.RunRemotingServer();

                mListener = new XmppListener();
                mScheduler = new Scheduler();

                mScheduler.StartScheduler();
                

                Thread t = new Thread(mListener.Main);
                t.Start();
            } catch (Exception ex)
            {
                EventLog.WriteEntry(sSource, ex.ToString(),
                EventLogEntryType.Error, 234);
                throw;
            }
        }

        protected override void OnStop()
        {
            mScheduler.StopScheduler();
            mListenerPing.Stop();
            mListener.ShutDown();
        }

        
    }
}
